var currentIndex;
$(document).ready(function(){
	initNote();
});
function initNote(){
	articleId = window.location.href.split("?")[1];
	var url = "/HyperIT/note/findOneNote";
	var parm = "";
	if(getCookie("user")==null){
		parm = "note_id="+articleId;
	}else{
		parm = "userdetail_id="+thisUser.id+"&note_id="+articleId;
	}
	$.get(url,parm,function(data){
		user = data.resultParm.user;
		authorId = user.id;
		note = data.resultParm.note;
		$(".note-about>h1").text(note.title);
		$("#noteAuthor").attr("href","../blog/theblog.html?"+user.id);
		$("#noteAuthor img").attr("src","../.."+user.head);
		$("#reading").text(note.reading);
		$("#collecting").text(note.collecting);
		$("#createtime").text("发布时间:"+note.createtime.substr(0,16));
		$("#noteContent").append(html_decode(note.content_html));
		editormd.markdownToHTML("noteContent");
		initNoteComment();
		if(data.resultParm.follow){
			$("#author-info>a").eq(0).text("取关").addClass("active").attr("onclick","cancelFollow(authorId)");
		}
		if(data.resultParm.collect){
			$(".action a").eq(0).text("取消收藏").addClass("active").attr("onclick","cancelCollect(articleId,0)");
		}
		$(".this-commer img").attr("src","../.."+thisUser.head);
	});
}
function getNoteComment(index){
	var url = "/HyperIT/blog/getComment";
	var parm = "size=20&indexPageNum="+index+"&target_id="+articleId+"&type=1";
	$.get(url,parm,function(data){
		if (data.serviceResult) {
			var commentList = data.resultParm.getcomment;
			showNoteComment(commentList);
			currentIndex++;
		} else{
			if (currentIndex==1) {
				return;
			} else{
				messageTip(data.resultInfo);
			}
		}
	});
}
function showNoteComment(commentList){
	for(var i = 0;i<commentList.length; i++){
		$(".comments .btn-box").before('<div class="comment-item">'+
						            	'<div class="commer-head">'+
							        		'<a href="../blog/theblog.html?'+commentList[i].userdetail.id+'">'+
							        		'<img src="../..'+commentList[i].userdetail.head+'"/></a>'+
						            	'</div>'+
										'<div class="comment-right">'+
											'<p>'+commentList[i].comment.content+'</p>'+
										'</div>'+
										'<div class="comment-time-action">'+
											'<span>'+commentList[i].comment.createtime.substr(0,16)+'</span>'+
										'</div>'+
									'</div>');
	}
}
//初始化comment
function initNoteComment(){
	$(".comments").children(".comment-item").remove();
	currentIndex = 1;
	getNoteComment(currentIndex);
}
function getMoreNoteComment(){
	getNoteComment(currentIndex);
}
//发表评论
function say(){
	if(getCookie("user")==null){
		messageTip("哎呀，还没登录呢！");
		return;
	}
	if($("#this-comment").val().trim()==""){
		messageTip("请输入评论内容");
		return;
	}
	var url = "/HyperIT/blog/comment";
	var parm = "userDetail_id="+thisUser.id+"&target_id="+articleId+"&content="+$("#this-comment").val()+"&type=1";
	$.post(url,parm,function(data){
		if (data.serviceResult) {
			var comment = data.resultParm.comment;
			$(".comments>h1").after('<div class="comment-item">'+
						            	'<div class="commer-head">'+
							        		'<a href="../blog/theblog.html?'+comment.userDetail_id+'">'+
							        		'<img src="../..'+thisUser.head+'"/></a>'+
						            	'</div>'+
										'<div class="comment-right">'+
											'<p>'+comment.content+'</p>'+
										'</div>'+
										'<div class="comment-time-action">'+
											'<span>'+comment.createtime.substr(0,16)+'</span>'+
										'</div>'+
									'</div>');
			$("#this-comment").val("");
			messageTip(data.resultInfo);
		} else{
			messageTip(data.resultInfo);
		}
	});
}